package com.google.code.netbuilder.dal.services;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.ConnectionPoolDataSource;

import com.google.code.netbuilder.dal.providers.*;
import com.google.code.netbuilder.dal.providersimpl.*;
//import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;

public class JDBCServise extends DAOServis {
//
//	private static MysqlConnectionPoolDataSource dataSource = null;
//	
//	static
//	{
//		try 
//		{
//			LocateRegistry.createRegistry(1099);
//		
//			System.out.println("RMI registry ready.");
//			dataSource = new MysqlConnectionPoolDataSource();
//			dataSource.setUser("user1");
//			dataSource.setPassword("123456");
//			dataSource.setServerName("localhost");
//			dataSource.setPort(3306);
//			dataSource.setDatabaseName("netbuilderdb");
//			
//			InitialContext context = createContext();
//			context.bind("HrDS", dataSource);
//			context.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//	
//	private static InitialContext createContext() throws NamingException {
//	    Properties env = new Properties();
//	    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
//	    env.put(Context.PROVIDER_URL, "rmi://localhost:1099");
//	    InitialContext context = new InitialContext(env);
//	    return context;
//	  }
//	
	
	public static Connection getConnection() throws SQLException
	{		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		}
		Connection conn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/netbuilderDB",
		                "root", "12345");
		         
		if (conn == null) 
		{
			System.out.println("No DB connection!!!");
		    throw new SQLException("No connection found");
		}
		else if (conn != null)
		{
			System.out.println("Connection is here: " + conn.getAutoCommit());                   
		}
		
		return conn;		
	}
	
	@Override
	public ICableProvider getCabelProvider() {
		return new CabelProviderJDBC();
	}

	@Override
	public ICableWireProvider getCableWireProvider() {
		return new CabelWireProviderJDBC();
	}

	@Override
	public IEquipmentPortProvider getEquipmentPortProvider() {
		return new EquipmentPortProviderJDBC();
	}

	@Override
	public IEquipmentProvider getEquipmentProvider() {
		return new EquipmentProviderJDBC();
	}

	@Override
	public IEquipmentVersion getEquipmentVersion() {
		return new EquipmentVersionJDBC();
	}

	@Override
	public IPortProvider getPortProvider() {
		return new PortProviderJDBC();
	}

	@Override
	public IWireProvider getWireProvider() {
		return new WireProviderJDBC();
	}

	@Override
	public IWireVersion getWireVersion() {
		return new WireVersionJDBC();
	}

}
